package com.ttg.web.redis;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.AutoConfigureBefore;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.data.redis.connection.RedisClusterConfiguration;
import org.springframework.data.redis.connection.RedisSentinelConfiguration;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.lettuce.LettuceConnectionFactory;

import java.io.Serializable;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * @Time 2022/5/19 11:12
 * @Version 1.0.0
 * @Author liujie
 * @Destination
 */
@Configuration
@AutoConfigureBefore(RedissonAutoConfig.class)
public class JedisConnectionFactoryBean implements Serializable {
    @Autowired
    private RedisProperties redisProperties;

    @Bean(value = "jedisConnectionFactory")
    @ConditionalOnMissingBean(LettuceConnectionFactory.class)
    @Order(value = Ordered.HIGHEST_PRECEDENCE)
    LettuceConnectionFactory jedisConnectionFactory( ) throws Exception {
        //单例模式-RedisStandaloneConfiguration  集群模式-RedisClusterConfiguration 哨兵模式-RedisSentinelConfiguration
         if(StringUtils.isBlank(redisProperties.getModel())||redisProperties.getModel().equals(RedisConstants.STAND_ALONE)){
             String[] host = redisProperties.getAddress().split(":");
             RedisStandaloneConfiguration configuration=new RedisStandaloneConfiguration(host[0]);
             configuration.setPassword(redisProperties.getPassword());
             LettuceConnectionFactory jedisConnectionFactory= new LettuceConnectionFactory(configuration);
             return jedisConnectionFactory;
         }
         if(redisProperties.getModel().equals(RedisConstants.MASTER_SLAVE)){
             String[] host = redisProperties.getAddress().split("\\,");
             RedisClusterConfiguration clusterConfiguration=new RedisClusterConfiguration(Arrays.asList(host));
             clusterConfiguration.setPassword(redisProperties.getPassword());
             LettuceConnectionFactory jedisConnectionFactory= new LettuceConnectionFactory(clusterConfiguration);
             return jedisConnectionFactory;
         }
        if(redisProperties.getModel().equals(RedisConstants.SENTRY)) {
            String[] host = redisProperties.getAddress().split("\\,");
            RedisSentinelConfiguration redisSentinelConfiguration = new RedisSentinelConfiguration(redisProperties.getMasterName(), Arrays.stream(host).collect(Collectors.toSet()));
            redisSentinelConfiguration.setPassword(redisProperties.getPassword());
            LettuceConnectionFactory jedisConnectionFactory = new LettuceConnectionFactory(redisSentinelConfiguration);
            return jedisConnectionFactory;
        }

        throw new  Exception("redisProperties model only four values for masterSlave or sentry or standAlone or empty");
    }
}
 